package com.fjut.zr.judge_system.VJ.VJS;

import com.fjut.zr.judge_system.POJO.PO.TVjResultPO;
import com.fjut.zr.judge_system.Util.HDUCodeLanguage;
import com.fjut.zr.judge_system.Util.MyClient;
import com.fjut.zr.judge_system.VJ.VjSubmitter;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HDU extends BaseVJ {
    public static Map<String, String> resultMap;
    static{
        resultMap = new HashMap<String, String>();
        resultMap.put("Queuing","JUDGING");
        resultMap.put("Compiling","JUDGING");
        resultMap.put("Running","JUDGING");
        resultMap.put("Accepted","Accepted");
        resultMap.put("Wrong Answer","Wrong Answer");
        resultMap.put("Runtime Error (ACCESS_VIOLATION)","Runtime Error");
        resultMap.put("Runtime Error (ARRAY_BOUNDS_EXCEEDED)","Runtime Error");
        resultMap.put("Runtime Error (FLOAT_DENORMAL_OPERAND)","Runtime Error");
        resultMap.put("Runtime Error (FLOAT_DIVIDE_BY_ZERO)","Runtime Error");
        resultMap.put("Runtime Error (FLOAT_OVERFLOW)","Runtime Error");
        resultMap.put("Runtime Error (FLOAT_UNDERFLOW)","Runtime Error");
        resultMap.put("Runtime Error (INTEGER_DIVIDE_BY_ZERO)","Runtime Error");
        resultMap.put("Runtime Error (INTEGER_OVERFLOW)","Runtime Error");
        resultMap.put("Runtime Error (STACK_OVERFLOW)","Runtime Error");
        resultMap.put("Time Limit Exceeded","Time Limit Exceeded");
        resultMap.put("Memory Limit Exceeded","Memory Limit Exceeded");
        resultMap.put("Output Limit Exceeded","Output Limit Exceeded");
        resultMap.put("Presentation Error","Presentation Error");
        resultMap.put("Compilation Error","Compilation Error");
        resultMap.put("System Error","System Error");
    }
    @Override
    public String getRid(VjSubmitter s) {
        //获取评测ID
        Document document;
        String statusurl = "https://acm.hdu.edu.cn/status.php?first=&pid=&user=fjutvjudge&lang=0&status=0";
        //解析网页（jsoup返回document就是浏览器里的document）连接超过十秒为超时
        try {
            document = Jsoup.parse(new URL(statusurl), 10000);
        } catch (IOException e) {
            return "error";
        }
        Element e = document.select("table:nth-child(2) tr:nth-child(4) table tr:nth-child(3)").first();
        return (e.select("td:nth-child(1)").first().text());
    }

    @Override
    public String submit(VjSubmitter s) {
        String url= "https://acm.hdu.edu.cn";
        MyClient hc=new MyClient();
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("username","fjutvjudge"));
        formparams.add(new BasicNameValuePair("userpass","fjutvjudge9999"));
        if(hc.Post(url+"/userloginex.php?action=login&cid=0&notice=0", formparams)==null) {
            System.out.println("==========================登录失败!");
            return "error";
        }
        List<NameValuePair> formparams1 = new ArrayList<NameValuePair>();
        int languageid=new HDUCodeLanguage().getLanguageId(s.info.getLanguage());
        formparams1.add(new BasicNameValuePair("language",Integer.toString(languageid)));
        formparams1.add(new BasicNameValuePair("problemid","" +s.info.getPid()));
        formparams1.add(new BasicNameValuePair("check", "0"));
        formparams1.add(new BasicNameValuePair("usercode",s.info.getCode()));
        if(hc.Post(url+"/submit.php?action=submit",formparams1)==null) {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    @Override
    public TVjResultPO getResult(VjSubmitter s) {
        TVjResultPO resultInfo=new TVjResultPO();
        Document document = null;
        String statusurl="https://acm.hdu.edu.cn/status.php?first=&pid=&user=fjutvjudge&lang=0&status=0";
        //解析网页（jsoup返回document就是浏览器里的document）连接超过十秒为超时
        try {
            document= Jsoup.parse(new URL(statusurl),10000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element e = document.select("table:nth-child(2) tr:nth-child(4) table tr:nth-child(3)").first();
        resultInfo.setRunId(e.select("td:nth-child(1)").first().text());
        resultInfo.setUser(e.select("td:nth-child(9)").first().text());
        resultInfo.setProblem(e.select("td:nth-child(4)").first().text());
        resultInfo.setResult(resultMap.get(e.select("td:nth-child(3)").first().text()));
        resultInfo.setMemory(e.select("td:nth-child(6)").first().text()+"B");
        resultInfo.setTime(e.select("td:nth-child(5)").first().text());
        resultInfo.setLanguage(e.select("td:nth-child(8)").first().text());
        resultInfo.setCodelength(e.select("td:nth-child(7)").first().text());
        resultInfo.setSubmitTime(e.select("td:nth-child(2)").first().text());
        if (resultInfo.getResult().equals("Compilation Error"))
        {
            resultInfo.setResultDetail(getCEInfo(resultInfo.getRunId()));
        }
        return resultInfo;
    }
    public String getCEInfo(String runid){
        Element e;
        Document d = null;
        try {
            d = Jsoup.connect("http://acm.hdu.edu.cn/viewerror.php?rid="+runid).get();
            return d.select("pre").first().html();
        } catch (IOException e1) {
            e1.printStackTrace();
            return "获取失败";
        } catch (NullPointerException e2){
            return "获取失败";
        }
    }
}
